package com.headwind.mail.impl;

import com.headwind.entity.HwUser;
import com.headwind.mail.SendJunkMailService;
import com.headwind.service.HwUserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * @author : Headwind
 * @version : 1.0
 * @date : Created in 2020/8/10
 * @description : 发送用户邮件服务
 */
@Service
public class SendJunkMailServiceImpl implements SendJunkMailService {

    @Resource
    JavaMailSender mailSender;
    @Resource
    private HwUserService userService;
    @Value("${spring.mail.username}")
    private String from;
    public static final Logger logger =
            LogManager.getLogger(SendJunkMailServiceImpl.class);

    @Override
    public boolean sendJunkMail(List<HwUser> userList) {

        try {
            if (userList == null || userList.size() <= 0) {
                return Boolean.FALSE;
            }
            for (HwUser user : userList) {
                MimeMessage mimeMessage = this.mailSender.createMimeMessage();
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                //邮件发送方
                message.setFrom(from);
                //邮件主题
                message.setSubject("地瓜今日特卖");
                //邮件接收方
                message.setTo("al_test@163.com");
                //邮件内容
                message.setText(user.getName() + " ,你知道么？厦门地瓜今日特卖，一斤只要9元");
                //发送邮件
                this.mailSender.send(mimeMessage);
            }
        } catch (Exception ex) {
            logger.error("sendJunkMail error and ayUser=%s", userList, ex);
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
